﻿using System;
using DAL;
using Entity;
using NLog;
public partial class Admin_AddUsers : ParentPage
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                if (Request.Url.AbsoluteUri.IndexOf("userEmailAddress=") >= 0)
                {
                    DALUsers objDALUsers = new DALUsers();
                    Users objUser = objDALUsers.GetUserInfo(Request.QueryString["userEmailAddress"].ToString());
                    txtUserFullName.Text = objUser.UserFullName;
                    txtEmailAddress.Text = Request.QueryString["userEmailAddress"].ToString();
                    ddlUserStstus.SelectedValue = objUser.UserStatus.ToString();
                    tr_UserMessage.InnerHtml = "Note: if you update the user profile you should reset the password";
                }
            }
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_AddCategory:::Page_Load:::" + ex.Message);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Users objUsers = new Users();
            DALUsers objSignUp = new DALUsers();
            objUsers.UserFullName = txtUserFullName.Text;
            objUsers.UserEmailAddress = txtEmailAddress.Text;
            objUsers.UserPassword = EncryptionMethods.Encryption.Encrypt(txtPassword.Text);
            objUsers.UserPermission = Convert.ToInt16(DDLUserPermission.SelectedValue);
            objUsers.UserStatus = Convert.ToInt16(ddlUserStstus.SelectedValue);
            if (Request.Url.AbsoluteUri.IndexOf("userEmailAddress=") >= 0)
            {
                objUsers.UserID = Request.QueryString["UserID"].ToString();
                int nReturnValue = objSignUp.EditUserInfo(objUsers);
                if (nReturnValue == 1)
                {
                    tr_UserMessage.InnerHtml = "Edit User Successfully";
                    string EditUserEmail = string.Format("User FullName: {0}<br />User Email Address: {1}<br />Password: {2}<br />User Permission:{3}<br />User Status:{4}", objUsers.UserFullName, objUsers.UserEmailAddress, txtPassword.Text, DDLUserPermission.SelectedItem.Text, ddlUserStstus.SelectedItem.Text);
                    FormsFunction.SendMail("khodrog@gmail.com", "Edit User", EditUserEmail);
                    EditUserEmail = string.Empty;
                    Response.Redirect("Users", false);
                }
                else
                {
                    tr_UserMessage.InnerHtml = "you have issues please contact the system support";
                }
            }
            else
            {
                int nReturnValue = objSignUp.SignUpUser(objUsers);
                if (nReturnValue == 1)
                {
                    tr_UserMessage.InnerHtml = "Add User Successfully";
                    string EditUserEmail = string.Format("User FullName: {0}<br />User Email Address: {1}<br />Password: {2}<br />User Permission:{3}<br />User Status:{4}", objUsers.UserFullName, objUsers.UserEmailAddress, txtPassword.Text, DDLUserPermission.SelectedItem.Text, ddlUserStstus.SelectedItem.Text);
                    FormsFunction.SendMail("khodrog@gmail.com", "Add User", EditUserEmail);
                    EditUserEmail = string.Empty;
                    Response.Redirect("Users", false);
                }
                else
                {
                    tr_UserMessage.InnerHtml = "you have issues please contact the system support";
                }
            }

        }
        catch (Exception ex)
        {
            _logger.Error("Admin_AddCategory:::btnSubmit_Click:::" + ex.Message);
        }
    }
}